package com.milo.easypoi.controller.export;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.milo.easypoi.entity.CompanyHasImgModel;
import com.milo.easypoi.entity.CompanyHasNetImgModel;
import com.milo.easypoi.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片导出
 * @author milogenius
 * @date 2020-10-21 11:00
 */
@Controller
@RequestMapping
@Slf4j
public class ImageExcelExport {


    /**
     * 初始化本地图片数据
     * @return
     */
    public List<CompanyHasImgModel> initData() {
        List<CompanyHasImgModel> list = new ArrayList<CompanyHasImgModel>();
        list.add(new CompanyHasImgModel("百度", "C:/Users/milogenius/Pictures/boy.jpg", "北京市海淀区西北旺东路10号院百度科技园1号楼"));
        list.add(new CompanyHasImgModel("阿里巴巴", "C:/Users/milogenius/Pictures/kid.jpg", "北京市海淀区西北旺东路10号院百度科技园1号楼"));
        list.add(new CompanyHasImgModel("Lemur", "C:/Users/milogenius/Pictures/river.jpg", "亚马逊热带雨林"));
        list.add(new CompanyHasImgModel("一众", "C:/Users/milogenius/Pictures/man.jpg", "山东济宁俺家"));
         return list;
    }

    /**
     * 初始化网络图片数据
     * @return
     */
    public List<CompanyHasNetImgModel> initNetData() {
        byte[] imageFromNetByUrl = HttpClientUtil.getImageFromNetByUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1603711739505&di=09552b8c3a91fc19de7d19795bf5b82d&imgtype=0&src=http%3A%2F%2Ft9.baidu.com%2Fit%2Fu%3D1307125826%2C3433407105%26fm%3D79%26app%3D86%26f%3DJPEG%3Fw%3D5760%26h%3D3240");
        CompanyHasNetImgModel companyHasNetImgModel = new CompanyHasNetImgModel("百度", imageFromNetByUrl, "北京市海淀区西北旺东路10号院百度科技园1号楼");
        List<CompanyHasNetImgModel> list = new ArrayList<CompanyHasNetImgModel>();
        list.add(companyHasNetImgModel);
        return list;
    }

    /**
     * 导出本地图片数据
     */
    @RequestMapping("localImage")
    @ResponseBody
    public void exportCompanyImg() {
        log.info("导出本地图片数据开始");
        //初始化数据
        List<CompanyHasImgModel> list = new ImageExcelExport().initData();
        try {
            File savefile = new File("E:/excel/");
            if (!savefile.exists()) {
                savefile.mkdirs();
            }
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("导出图片属性", "测试"), CompanyHasImgModel.class, list);
            FileOutputStream fos = new FileOutputStream("E:/excel/image.xlsx");
            workbook.write(fos);
            fos.close();
            log.info("导出本地图片数据结束");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 导出网络图片数据
     */
    @RequestMapping("netImage")
    @ResponseBody
    public void exportNetImage() {
        log.info("导出网络图片数据开始");
        //初始化数据
        List<CompanyHasNetImgModel> list = new ImageExcelExport().initNetData();
        try {
            File savefile = new File("E:/excel/");
            if (!savefile.exists()) {
                savefile.mkdirs();
            }
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("导出图片属性", "测试"), CompanyHasNetImgModel.class, list);
            FileOutputStream fos = new FileOutputStream("E:/excel/netImage.xlsx");
            workbook.write(fos);
            fos.close();
            log.info("导出网络图片数据结束");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
